package soeasy.model.neuron;

import java.util.Iterator;


import repast.simphony.engine.schedule.IAction;
import repast.simphony.engine.schedule.ScheduleParameters;
import soeasy.amas.Action;
import soeasy.amas.CooperativeAgent;
import soeasy.model.FInstantFrequency;
import soeasy.model.SOEASYParameters;
import soeasy.model.Spike;

public class RestingNeuron extends Neuron {

	public RestingNeuron(CooperativeAgent creatorAgent, String identifier,
			boolean isExcitatory, int numberOfNeurons, double axonalDelay,
			double ahpLevel, String cause) {
		super(creatorAgent, identifier, isExcitatory, numberOfNeurons,
				axonalDelay, ahpLevel, cause);
	}

	@Override
	protected void defineActions() {
		super.defineActions();

		defineFeedbackActionPair(FInstantFrequency.class,
				ACHandleFInstantFrequency4RestingNeuron.class);

		Action action = new ACCheckDepolarization(this);
		double currentTick = SOEASYParameters.getInstance()
				.getCurrentTick();
		scheduleRepeatingAction(currentTick + 1, 3000, 0, action);
	}

//	@Override
//	public double getCriticality() {
//		double criticality = 0.0;
//
//		double currentTick = SOEASYParameters.getInstance()
//				.getCurrentTick();
//		if (currentTick - lastSpikeTime > 3000) {
//			criticality = 100.0;
//		} else {
//			double sumUp = 0.0;
//			double sumDown = 0.0;
//			Iterator<Synapse> iterator = getPostSynapses().iterator();
//			while (iterator.hasNext()) {
//				Synapse postSynapse = iterator.next();
//				double synapseCriticality = postSynapse.getCriticality();
//				double deltaCriticality = postSynapse.getDeltaCriticality();
//				sumUp += Math.max(deltaCriticality, synapseCriticality);
//				sumDown++;
//			}
//			if (sumDown != 0.0) {
//				criticality = sumUp / sumDown;
//			}
//		}
//
//		if (Double.isNaN(criticality)) {
//			criticality = 0.0;
//		}
//		return criticality;
//	}

	@Override
	protected void excite() {
		super.excite();

		double currentTime = SOEASYParameters.getInstance()
				.getCurrentTick();
		// make this neuron go back to resting state after
		// "amplitude" time steps.
		SOEASYParameters.schedule(ScheduleParameters.createOneTime(
				currentTime + Spike.SPIKE_DURATION, 0, 0.0), new IAction() {
			public void execute() {
				setPotential(Spike.FIRING_THRESHOLD - getAhpLevel());
			}
		});

	}

}
